package com.yykj.order.center.server.controller;

import com.yykj.common.vo.ResultVO;
import com.yykj.order.center.client.dto.ConsultationAdviseDto;
import com.yykj.order.center.client.dto.ConsultationCreateDto;
import com.yykj.order.center.client.dto.ConsultationDoctorDto;
import com.yykj.order.center.client.dto.ConsultationDoctorRankingDto;
import com.yykj.order.center.client.dto.ConsultationInfoDto;
import com.yykj.order.center.client.dto.ConsultationListDto;
import com.yykj.order.center.client.dto.base.PaginationListDto;
import com.yykj.order.center.client.form.consultation.ConsultationAddDoctorForm;
import com.yykj.order.center.client.form.consultation.ConsultationAdviseForm;
import com.yykj.order.center.client.form.consultation.ConsultationChargebackForm;
import com.yykj.order.center.client.form.consultation.ConsultationConclusionForm;
import com.yykj.order.center.client.form.consultation.ConsultationCreateForm;
import com.yykj.order.center.client.form.consultation.ConsultationDoctorQuitForm;
import com.yykj.order.center.client.form.consultation.ConsultationGetAdviseForm;
import com.yykj.order.center.client.form.consultation.ConsultationGetByGroupImIdForm;
import com.yykj.order.center.client.form.consultation.ConsultationListForm;
import com.yykj.order.center.client.form.consultation.ConsultationRemoveDoctorForm;
import com.yykj.order.center.server.service.ConsultationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 在线会诊 订单
 */
@RestController
@RequestMapping("/api/consultation")
@Api(tags = "在线会诊")
public class ConsultationController {

    @Autowired
    ConsultationService consultationService;

    //TODO applyDoctorId 需要从token里拿

    @PostMapping("/create")
    @ApiOperation(value = "创建会诊")
    public ResultVO<ConsultationCreateDto> create(@RequestBody @Valid ConsultationCreateForm consultationCreateForm) {
        return ResultVO.success(consultationService.create(consultationCreateForm));
    }

    @PostMapping("/list")
    @ApiOperation(value = "当前登录医生的会诊列表")
    public ResultVO<PaginationListDto<ConsultationListDto>> list(@RequestBody @Valid ConsultationListForm consultationListForm) {
        return ResultVO.success(consultationService.list(consultationListForm));
    }

    @GetMapping("/{consultationId}")
    @ApiOperation(value = "会诊详情")
    public ResultVO<ConsultationInfoDto> info(@PathVariable(value = "consultationId", required = true) String consultationId) {
        return ResultVO.success(consultationService.info(consultationId));
    }

    @PostMapping("/getByGroupImId")
    @ApiOperation(value = "会诊详情")
    public ResultVO<ConsultationInfoDto> getByGroupImId(@RequestBody @Valid ConsultationGetByGroupImIdForm consultationGetByGroupImIdForm) {
        return ResultVO.success(consultationService.getByGroupImId(consultationGetByGroupImIdForm.getGroupImId()));
    }

    @PostMapping("/chargeback")
    @ApiOperation(value = "申请医生自己退单")
    public ResultVO chargeback(@RequestBody @Valid ConsultationChargebackForm consultationChargebackForm) {
        consultationService.chargeback(consultationChargebackForm);
        return ResultVO.success();
    }

    @PostMapping("/addDoctor")
    @ApiOperation(value = "添加与会医生")
    public ResultVO addDoctor(@RequestBody @Valid ConsultationAddDoctorForm consultationAddDoctorForm) {
        consultationService.addDoctor(consultationAddDoctorForm);
        return ResultVO.success();
    }

    @PostMapping("/removeDoctor")
    @ApiOperation(value = "申请医生 删除 与会医生")
    public ResultVO removeDoctor(@RequestBody @Valid ConsultationRemoveDoctorForm consultationRemoveDoctorForm) {
        consultationService.removeDoctor(consultationRemoveDoctorForm);
        return ResultVO.success();
    }

    @PostMapping("/doctorQuit")
    @ApiOperation(value = "与会医生自己退出")
    public ResultVO doctorQuit(@RequestBody @Valid ConsultationDoctorQuitForm consultationDoctorQuitForm) {
        consultationService.doctorQuit(consultationDoctorQuitForm);
        return ResultVO.success();
    }

    @GetMapping("/doctorList/{consultationId}")
    @ApiOperation(value = "查看与会医生列表")
    public ResultVO<List<ConsultationDoctorDto>> doctorList(@PathVariable(value = "consultationId", required = true) String consultationId) {
        return ResultVO.success(consultationService.doctorList(consultationId));
    }

    @PostMapping("/advise")
    @ApiOperation(value = "与会医生发表会诊建议")
    public ResultVO advise(@RequestBody @Valid ConsultationAdviseForm consultationAdviseForm) {
        consultationService.advise(consultationAdviseForm);
        return ResultVO.success();
    }

    @PostMapping("/getAdvise")
    @ApiOperation(value = "与会医生获取自己的会诊建议")
    public ResultVO<ConsultationAdviseDto> getAdvise(@RequestBody @Valid ConsultationGetAdviseForm consultationGetAdviseForm) {
        return ResultVO.success(consultationService.getAdvise(consultationGetAdviseForm));
    }

    @PostMapping("/conclusion")
    @ApiOperation(value = "申请医生发表会诊结论, 同时结束这单会诊")
    public ResultVO conclusion(@RequestBody @Valid ConsultationConclusionForm consultationConclusionForm) {
        consultationService.conclusion(consultationConclusionForm);
        return ResultVO.success();
    }

    @GetMapping("/adviseList/{consultationId}")
    @ApiOperation(value = "与会医生会诊建议列表")
    public ResultVO<List<ConsultationAdviseDto>> adviseList(@PathVariable(value = "consultationId", required = true) String consultationId) {
        return ResultVO.success(consultationService.adviseList(consultationId));
    }

    @GetMapping("/doctor/ranking/{doctorId}")
    @ApiOperation(value = "医生常用医生列表")
    public ResultVO<List<ConsultationDoctorRankingDto>> doctorRanking(@PathVariable("doctorId") String doctorId) {
        return ResultVO.success(consultationService.doctorRanking(doctorId));
    }
}